﻿using System;
using ComputerBeacon.Csv;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1 {
    [TestClass]
    public class TableTest {
        [TestMethod]
        public void OneLine() {
            var csv = new CsvTable("a,b,c");
            Assert.AreEqual(1, csv.Rows.Count);
            Assert.AreEqual(3, csv.ColumnsCount);
            Assert.AreEqual("a", csv.Rows[0][0]);
            Assert.AreEqual("b", csv.Rows[0][1]);
            Assert.AreEqual("c", csv.Rows[0][2]);
            for (int row = 0; row < 1; row++) for (int col = 0; col < 3; col++) Assert.AreEqual(csv.Rows[row][col], csv[row, col]);
        }
        [TestMethod]
        public void TwoLines() {
            var csv = new CsvTable("a,b,c\r\nd,e,f");
            Assert.AreEqual(2, csv.Rows.Count);
            Assert.AreEqual(3, csv.ColumnsCount);
            Assert.AreEqual("a", csv.Rows[0][0]);
            Assert.AreEqual("b", csv.Rows[0][1]);
            Assert.AreEqual("c", csv.Rows[0][2]);
            Assert.AreEqual("d", csv.Rows[1][0]);
            Assert.AreEqual("e", csv.Rows[1][1]);
            Assert.AreEqual("f", csv.Rows[1][2]);
            for (int row = 0; row < 2; row++) for (int col = 0; col < 3; col++) Assert.AreEqual(csv.Rows[row][col], csv[row, col]);
        }
        [TestMethod]
        public void WithHeader() {
            var csv = new CsvTable("Year,Make,Model\r\n1997,Ford,E350\r\n2000,Mercury,Cougar", true);
            Assert.AreEqual(2, csv.Rows.Count);
            Assert.AreEqual(3, csv.ColumnsCount);
            Assert.AreEqual("1997", csv.Rows[0][0]);
            Assert.AreEqual("Ford", csv.Rows[0][1]);
            Assert.AreEqual("E350", csv.Rows[0][2]);
            Assert.AreEqual("2000", csv.Rows[1][0]);
            Assert.AreEqual("Mercury", csv.Rows[1][1]);
            Assert.AreEqual("Cougar", csv.Rows[1][2]);
            Assert.AreEqual("Year", csv.Headers[0]);
            Assert.AreEqual("Make", csv.Headers[1]);
            Assert.AreEqual("Model", csv.Headers[2]);
            for (int i = 0; i < 2; i++) {
                Assert.AreEqual(csv.Rows[i][0], csv[i, "Year"]);
                Assert.AreEqual(csv.Rows[i][1], csv[i, "Make"]);
                Assert.AreEqual(csv.Rows[i][2], csv[i, "Model"]);
            }
            for (int row = 0; row < 2; row++) for (int col = 0; col < 3; col++) Assert.AreEqual(csv.Rows[row][col], csv[row, col]);
        }
        [TestMethod]
        public void AllowTrailingEmptyLine() {
            var csv = new CsvTable("a,b,c\r\nd,e,f\r\n");
            Assert.AreEqual(2, csv.Rows.Count);
            Assert.AreEqual(3, csv.ColumnsCount);
            Assert.AreEqual("a", csv.Rows[0][0]);
            Assert.AreEqual("b", csv.Rows[0][1]);
            Assert.AreEqual("c", csv.Rows[0][2]);
            Assert.AreEqual("d", csv.Rows[1][0]);
            Assert.AreEqual("e", csv.Rows[1][1]);
            Assert.AreEqual("f", csv.Rows[1][2]);
            for (int row = 0; row < 2; row++) for (int col = 0; col < 3; col++) Assert.AreEqual(csv.Rows[row][col], csv[row, col]);
        }
    }
}
